package com.kuizii.demo.web.interceptor;


import com.kuizii.demo.api.SysUserService;
import com.kuizii.demo.domain.SysUserDto;
import com.kuizii.demo.web.util.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 全局拦截器
 *
 * @author jameszhou
 */
@Component
public class GlobalInterceptor implements HandlerInterceptor {


    @Autowired
    Environment environment;

    @Autowired
    SysUserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        if (handler instanceof HandlerMethod) {

            /**
             * 保存登录信息
             */

            SysUserDto me = ShiroUtil.getSessionUser();
            String requestURI = request.getRequestURI();
            if (me == null) {
                return true;
            } else if (!me.getEmailVerified() && !me.getMobileVerified()) {
                SysUserDto userDto = userService.getUserById(me.getId());
                if (!userDto.getEmailVerified() && !userDto.getMobileVerified()) {
                    return false;
                }
            }
            me.setPassword("");
        }

        /**
         * 通过拦截
         */
        return true;
    }

}
